<?php
/**
 * Created by PhpStorm.
 * User: liuyu
 * Date: 2018/11/19
 * Time: 11:19
 */

namespace app\api\controller\v5;

use app\api\controller\v2\Auth;
use app\common\lib\astrolog\FortunesChange;
use app\common\model\AwardUser;
use app\common\model\Orders;
use app\common\model\Recode;

class FortuneForYear extends Auth
{
    protected $year_id;

    public function _initialize()
    {
        parent::_initialize();

        $this->year_id = config('goods.year_id');
    }

    /**
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     */
    public function index()
    {
        if (!request()->isGet())
        {
            return show(config('code.error'), '未知操作', [], 403);
        }

        $uid = $this->user['id'];
        $rid = input('param.rid');
        $year = input('param.year');
        //当前查看的时间
        $now_time = strtotime($year . '-1');

        //日历显示时间范围
        $range_start = config('fortune.fortune_time_range')['year']['start'];
        $range_end = config('fortune.fortune_time_range')['year']['end'];
        //解锁状态
        $unlock_status = 0;

        $map = ['uid' => $uid, 'id' => $rid];

        $OrdersModel = new Orders();
        $AwardUserModel = new AwardUser();
        $RecodeModel = new Recode();

        //获取档案信息
        $recode_info = $RecodeModel->getRecode($map);

        if (!$recode_info || $recode_info['uid'] != $this->user['id'])
        {
            return show(config('code.error'), '档案不存在');
        }

        //获取档案档案购买的订单
        $orders = $OrdersModel->fortune_orders(['uid' => $uid, 'rid' => $rid, 'type' => 3]);

        //购买订单解锁的时间
        $unlock_time = [];
        $order_time = [];
        $unlock_time_range = [];

        if ($orders && !empty($orders))
        {
            foreach ($orders as $k => $v)
            {
                if ($v['type'] == 3)
                {
                    $extend = json_decode($v['extend'], true);
                    if (isset($extend['expire_time']) && $extend['expire_time'])
                    {
                        $v['expire_time'] = $extend['expire_time'];
                    }
                    $start_time = date('Y', $v['time']);
                    $end_time = date('Y', strtotime('-1 days', strtotime($v['expire_time'], $v['time'])));

                    $unlock_time['start_time'] = $start_time;
                    $unlock_time['end_time'] = $end_time;

                    // 获取当前档案已解锁的时间
                    $order_time [] = prDates([
                        'start' => $unlock_time['start_time'],
                        'end' => $unlock_time['end_time'],
                        'type' => 3,
                    ]);

                    if (strtotime($start_time . '-1') <= $now_time && $now_time <= strtotime($end_time . '-1'))
                    {
                        $unlock_status = 1;

                        if ($v['status'] == 4)
                        {
                            $unlock_status = 2;
                        }
                    }
                }
            }
            foreach ($order_time as $k => $v)
            {
                foreach ($v as $vv) $unlock_time_range[] = $vv;
            }
        }
        // 获取当前用户的档案id
        $selfId = $RecodeModel->getSelfRecode(['uid' => $this->user['id']]);
        //获取中奖纪录
        $aorders = $AwardUserModel->get_award_fortune(['uid' => $this->user['id'], 'rid' => $rid, 'type' => 4, 'self_id' => $selfId]);

        //用户中奖解锁的时间
        $award_time = [];
        $unlock_award_times = [];
        $award_time_range = [];

        if ($aorders && !empty($aorders))
        {
            $date = date('Y', $now_time);
            foreach ($aorders as $k => $v)
            {
                $extend = json_decode($v['extend'], true);
                if (in_array($v['aid'], $this->year_id))
                {
                    $start_time = date('Y', $extend['award_time']);
                    $end_time = date('Y', strtotime('-1 days', strtotime($v['expire_time'], $extend['award_time'])));

                    $award_time['start_time'] = $start_time;
                    $award_time['end_time'] = $end_time;

                    // 获取当前档案已解锁的时间
                    $unlock_award_time = prDates([
                        'start' => $award_time['start_time'],
                        'end' => $award_time['end_time'],
                        'type' => 3,
                    ]);
                    if (in_array($date, $unlock_award_time) && $v['status'] == 1)
                    {
                        $unlock_status = 2;
                    } elseif (in_array($date, $unlock_award_time))
                    {
                        $unlock_status = 1;
                    }

                    $unlock_award_times[] = $unlock_award_time;
                }

            }
            if ($unlock_award_times)
            {
                foreach ($unlock_award_times as $k => $v)
                {
                    foreach ($v as $vv)
                    {
                        $award_time_range[] = $vv;
                    }
                }
            }
        }
        //合并时间
        $time_range = array_merge($unlock_time_range, $award_time_range);

        //时间去重
        $time_range = array_unique($time_range);
        //重新排序
        asort($time_range);
        //重新索引
        $time_range = array_values($time_range);

        //统计订单人数
        $fortune_base_num = $OrdersModel->fortune_base(['type' => 2]);

        if ($unlock_status == 2)
        {
            $fortune = (new FortunesChange())->init($recode_info, $year, 'year');
        } else
        {
            // 未解锁||解锁中 返回空值
            $fortune = [
                'fortune_year_change' => [],
                'fortune_year_stage' => [],
                'fortune_year_summarize' => [],
                'fortune_year_outline' => [],
            ];
        }

        $data = [
            'time' => $time_range,
            'fortune_year_summarize' => $fortune['fortune_year_summarize'],
            'fortune_year_outline' => $fortune['fortune_year_outline'],
            'fortune_year_change' => $fortune['fortune_year_change'],
            'fortune_year_stage' => $fortune['fortune_year_stage'],
            'unlock_num' => (int)($fortune_base_num),
            'unlock_status' => $unlock_status,
            'time_range' => [
                'start_time' => $range_start,
                'end_time' => $range_end,
            ],
        ];

        return show(config('code.success'), '加载成功', $data);

    }
}
